﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace WeChatDataSrc
{
    /// <summary>
    /// 全省文字报获取及上传
    /// </summary>
    public static class ForecastMessage
    {
        static string Dir = ConfigurationSettings.AppSettings["ForeMsgDirectory"];
        static string User = ConfigurationSettings.AppSettings["ForeMsgUser"];
        static string Pwd = ConfigurationSettings.AppSettings["ForeMsgPwd"];
        static string URL = ConfigurationSettings.AppSettings["UploadForeMsgURL"];
        static string Start = ConfigurationSettings.AppSettings["SubForeMsgStart"];
        static string End = ConfigurationSettings.AppSettings["SubForeMsgEnd"];

        static RemoteHost host = new RemoteHost(Dir, User, Pwd);

        public static void GetMsg(DateTime? sd)
        {

            DateTime NowMaxDateTime;

            if (DateTime.TryParse(ReadMax.ReadMaxText(), out NowMaxDateTime) == false)
            {
                NowMaxDateTime = DateTime.Now;
            }

            if (sd.HasValue)
            {
                NowMaxDateTime = Convert.ToDateTime(sd);
            }

            DirectoryInfo info = new DirectoryInfo(Dir);

            TimeSpan Span = DateTime.Now - NowMaxDateTime;
            for (int i = 0; i <= Span.Days; i++)
            {
                string postData = "";
                IOrderedEnumerable<FileInfo> NewestFile16List = info.GetFiles("JS" + NowMaxDateTime.ToString("yyMMdd") + ".16h").OrderBy(n => n.Name);
                if (NewestFile16List != null && NewestFile16List.Count() != 0)
                {
                    FileInfo NewestFile16 = NewestFile16List.Last();
                    string Msg = File.ReadAllText(NewestFile16.FullName, Encoding.Default);
                    Msg = Msg.Substring(Msg.IndexOf(Start), Msg.IndexOf(End) - Msg.IndexOf(Start));
                    postData = string.Format("data={0}&type={1}&station=all&date={2}", Msg, "晚上",NowMaxDateTime.ToString("yyyy-MM-dd"));
                    string result = HttpPost.SendData(URL, postData);
                }
                IOrderedEnumerable<FileInfo> NewestFile10List = info.GetFiles("JS" + NowMaxDateTime.ToString("yyMMdd") + ".10h").OrderBy(n => n.Name);
                if (NewestFile10List != null && NewestFile10List.Count() != 0)
                {
                    FileInfo NewestFile10 = NewestFile10List.Last();
                    string Msg = File.ReadAllText(NewestFile10.FullName, Encoding.Default);
                    Msg = Msg.Substring(Msg.IndexOf(Start), Msg.IndexOf(End) - Msg.IndexOf(Start));
                    postData = string.Format("data={0}&type={1}&station=all&date={2}", Msg, "下午",NowMaxDateTime.ToString("yyyy-MM-dd"));
                    string result = HttpPost.SendData(URL, postData);
                }
                try
                {
                    FileInfo NewestFile05 = info.GetFiles("JS" + NowMaxDateTime.ToString("yyMMdd") + ".05h").OrderBy(n => n.Name).Last();
                    string Msg = File.ReadAllText(NewestFile05.FullName, Encoding.Default);
                    Msg = Msg.Substring(Msg.IndexOf(Start), Msg.IndexOf(End) - Msg.IndexOf(Start));
                    postData = string.Format("data={0}&type={1}&station=all&date={2}", Msg, "上午", NowMaxDateTime.ToString("yyyy-MM-dd"));
                    string result = HttpPost.SendData(URL, postData);
                }
                catch
                {
                    postData = string.Format("data={0}&type={1}&station=all&date={2}", "", "上午", NowMaxDateTime.ToString("yyyy-MM-dd"));
                    string result = HttpPost.SendData(URL, postData);
                }

                
                NowMaxDateTime = NowMaxDateTime.AddDays(1);
            }
        }

        public static void UploadMsg(DateTime? sd)
        {
            host.Connect();
            GetMsg(sd);
            host.Disconnect();
        }

    }
}
